package com.qingxin.wuye.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.wuye.entity.WyBuilding;
import com.qingxin.wuye.entity.WyGarden;
import com.qingxin.wuye.entity.WyRoomFee;
import com.qingxin.wuye.service.WyBuildingService;
import com.qingxin.wuye.service.WyGardenService;
import com.qingxin.wuye.service.WyRoomFeeService;

@Controller
@RequestMapping("/roomFee")
public class RoomFeeAction extends BaseAction {

	@Autowired
	private WyRoomFeeService roomFeeService;
	
	@Autowired
	private WyGardenService gardenService;
	@Autowired
	private WyBuildingService buildingService;
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "wuye/roomFee/roomFee_list";
	}
	
	
	/**
	 * 获取列表分页数据
	 * @param datatype
	 * @param leftdata
	 * @param roomid
	 * @param roomCode
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, String roomid, String roomCode, PageParam pageParam, HttpServletRequest request){
		String gardenid = null;
		String buildingid = null;
		
		if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(datatype)){
			gardenid = leftdata;
		}else if(SystemCfg.UI_DATATYPE_BUILDING.equalsIgnoreCase(datatype)){
			buildingid = leftdata;
		}
		
		return roomFeeService.list(gardenid, buildingid, roomid, roomCode, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加信息
	 * @param model
	 * @param buildingid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String buildingid, HttpServletRequest request){
		model.put("buildingid", buildingid);
		
		WyBuilding building = buildingService.get(buildingid);
		WyGarden garden = gardenService.get(building.getWyGarden().getGardenid());
		model.put("companyid", garden.getWyPropertyCompany().getCompanyid());
		
		return "wuye/roomFee/roomFee_add";
	}
	
	
	/**
	 * 保存
	 * @param roomFee
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WyRoomFee roomFee, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = roomFeeService.add(roomFee, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改
	 * @param model
	 * @param feeid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String feeid){
		WyRoomFee roomFee = roomFeeService.get(feeid);
		model.put("roomFee", roomFee);
		
		return "wuye/roomFee/roomFee_edit";
	}
	
	
	/**
	 * 保存修改
	 * @param roomFee
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid WyRoomFee roomFee, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = roomFeeService.update(roomFee, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 删除
	 * @param feeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] feeid, HttpServletRequest request){
		if(feeid==null || feeid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return roomFeeService.delete(feeid, getLoginUser(request), request);
	}
	
	
}
